package com.northpool.operator.param;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.northpool.operator.param.custom.DatasetParam;

/**
 * 数据集参数
 */
public class DBParameter extends Parameter<DatasetParam> {

    public DBParameter(DatasetParam value) {
        super(value);
        this.type = Type.DB;
    }

    public DBParameter(DatasetParam value, boolean isDefault) {
        super(value, isDefault);
        this.type = Type.DB;
    }

    @Override
    @JSONField(serialize = false)
    public String getStringValue() {
        return JSONObject.toJSONString(this.value);
    }

    public static Parameter<?> parse(String strVal) {
        if (StrUtil.isBlank(strVal)) {
            return null;
        }
        try {
            DatasetParam datasetParam = JSONObject.parseObject(strVal, DatasetParam.class);
            return new DBParameter(datasetParam);
        } catch (Exception ex) {
            Console.log("数据源参数解析错误：" + ex.getMessage());
            return null;
        }
    }
}
